package com.h2.ref.server.web.social.facebook;

import org.springframework.social.connect.Connection;
import org.springframework.social.facebook.api.Facebook;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.h2.ref.server.user.IUserService;
import com.h2.ref.server.user.model.User;
import com.h2.ref.server.web.ViewConstants;
import com.h2.ref.server.web.social.ConnectionRepositoryInMem;

@Controller
public class FacebookController {
   
   private ConnectionRepositoryInMem _connectionDao;
   
   private IUserService _userService;
   
   ////
   ////
   
   @RequestMapping(ViewConstants.SETTINGS_FACEBOOK_PROFILE)
   public String profile(Model model) {
      
      User user = getUserService().getActiveUser();
      Connection<Facebook> connection =
            getConnectionDao().findPrimaryConnection(user, Facebook.class);
//      Facebook facebook = connection != null ? connection.getApi() : new FacebookTemplate();
      if (connection == null) {
         return ViewConstants.REDIRECT
               + ViewConstants.SETTINGS_FACEBOOK_PROFILE + ".html";
      }
      model.addAttribute("profile", connection.getApi().userOperations().getUserProfile());
      return ViewConstants.SETTINGS_FACEBOOK_PROFILE_VIEW;
   }

   /**
    * @return the connectionDao
    */
   public ConnectionRepositoryInMem getConnectionDao() {
      return _connectionDao;
   }

   /**
    * @param connectionDao the connectionDao to set
    */
   public void setConnectionDao(ConnectionRepositoryInMem connectionDao) {
      _connectionDao = connectionDao;
   }

   /**
    * @return the userService
    */
   public IUserService getUserService() {
      return _userService;
   }

   /**
    * @param userService the userService to set
    */
   public void setUserService(IUserService userService) {
      _userService = userService;
   }

}
